/*
* 2019-11-18
* 作用：用户相关状态存储
* */
// 状态即是数据
var state = {
  IsRefresh: true, // 判断页面是否刷新
  isLoading: false,
  isLogin: window.sessionStorage.getItem('isLogin') ? Boolean(window.sessionStorage.getItem('isLogin')) : false,
  token: window.sessionStorage.getItem('token') || '',
  menuInfo: (window.sessionStorage.getItem('menuInfo') || window.sessionStorage.getItem('menuInfo') !== 'undefined') ? JSON.parse(window.sessionStorage.getItem('menuInfo')) : [],
  userInfo: (window.sessionStorage.getItem('userInfo') || window.sessionStorage.getItem('userInfo') !== 'undefined') ? JSON.parse(window.sessionStorage.getItem('userInfo')) : [],
  company: (window.sessionStorage.getItem('company') || window.sessionStorage.getItem('company') !== 'undefined') ? JSON.parse(window.sessionStorage.getItem('company')) : []

}

// 定义getters 读取状态
var getters = {
  getRefresh (state) {
    return state.IsRefresh
  },
  getLoading (state) {
    return state.isLoading
  },
  getIsLogin (state) {
    return state.isLogin
  },
  getToken (state) {
    if (state.isLogin) {
      return state.token
    } else {
      return ''
    }
  },
  getCompany (state) {
    return state.company
  },
  getMenuInfo (state) {
    return state.menuInfo
  },
  getUserInfo (state) {
    return state.userInfo
  }
}

// 定义action,要执行的操作，如流程判断，异步请求等
var actions = {
  setNoRefresh ({ commit }) {
    commit('setNoRefresh')
  },
  setToken ({ commit }, token) {
    commit('SetToken', token) // 与mutations里的名称对应
  },
  loginSuccess ({ commit }, data) {
    commit('LoginSuccess', data)
  },
  loginEdit ({ commit }) {
    commit('LoginEdit')
  },
  loadingStart ({ commit }) {
    commit('LoadingStart') // 与mutations里的名称对应
  },
  loadingEnd ({ commit }) {
    commit('LoadingEnd') // 与mutations里的名称对应
  }
}

// 处理状态的改变
var mutations = {
  setNoRefresh (state) {
    state.IsRefresh = false // 设置不刷新
  },
  SetToken (state, token) { // 设置token
    state.token = token
    window.sessionStorage.setItem('token', token)
    // console.log(window.sessionStorage.getItem('token'))
  },
  LoginSuccess (state, data) { // 登录成功
    state.isLogin = true
    switch (data.length) {
      case 2: state.menuInfo = data[1]; state.userInfo = data[0]; state.company = data[0]; return
      case 3: state.menuInfo = data[2]; state.userInfo = data[1]; state.company = data[0]; return
    }
    window.sessionStorage.setItem('isLogin', true)
    window.sessionStorage.setItem('menuInfo', JSON.stringify(state.menuInfo))
    window.sessionStorage.setItem('userInfo', JSON.stringify(state.userInfo))
    window.sessionStorage.setItem('company', JSON.stringify(state.company))
    // Set('menuInfo', state.menuInfo)
    // Set('userInfo', state.userInfo)
  },
  LoginEdit (state) { // 注销
    state.isLogin = false
    state.token = ''
    state.roleData = []
    state.userInfo = []
    window.sessionStorage.setItem('isLogin', false)
    window.sessionStorage.removeItem('userInfo')
    window.sessionStorage.removeItem('menuInfo')
    window.sessionStorage.removeItem('token')
    window.sessionStorage.removeItem('company')
  },
  LoadingStart (state) {
    state.isLoading = true
  },
  LoadingEnd (state) {
    state.isLoading = false
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
